package com.coocaa.ops.admin.v1.audit.validator;


import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.audit.model.ExecutionTaskModel;
import com.coocaa.ops.admin.v1.audit.provider.BaseAuditTaskProviderImpl;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.base.model.ResourceBaseModel;
import com.coocaa.ops.common.tool.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * @author bijiahao
 * @date : 2018/6/15.
 * @description: 审核任务相关的校验
 */
@Component
public class AuditTaskValidator {
    /**
     * 版本是否可以被作为查询参数
     *
     * @param version
     * @return
     */
    public static boolean isVersionCanUseForQueryParam(Integer version) {
        return null != version && version > 0;
    }
    private static CommonLanguageProperties commonLanguageProperties;
    /**
     * 校验审核的版本，正常情况下，审核操作的版本，和当前原始表的当前版本，是一致的
     *
     * @param inputVersion
     * @param duplicateVersion
     */
    public static void validAuditOpVersion(String inputVersion, String duplicateVersion) {
        if (BaseAuditTaskProviderImpl.V_1.equals(inputVersion)) {
            //V1不需要校验
            return;
        }
        if (duplicateVersion == null || !duplicateVersion.equals(inputVersion)) {
            throw new BusinessException(null, "The reviewed version is inconsistent, please contact the developer");
        }
    }

    /**
     * 校验资源ID-版本-类型
     *
     * @param resourceBaseModel
     */
    public static void validIdAndVersionAndType(ResourceBaseModel resourceBaseModel) {
        Assert.notNull(resourceBaseModel.getId(), commonLanguageProperties.getResourceId()+commonLanguageProperties.getNotNull());
        Assert.hasText(resourceBaseModel.getType(), commonLanguageProperties.getResourceType()+commonLanguageProperties.getNotNull());
        Assert.notNull(resourceBaseModel.getVersion(), commonLanguageProperties.getResourceVersion()+commonLanguageProperties.getNotNull());
    }

    /**
     * 校验审核参数-资源ID-版本-类型-审核状态-审核备注
     *
     * @param executionTaskModel
     */
    public static void validAuditTask(ExecutionTaskModel executionTaskModel) {
        Assert.notNull(executionTaskModel.getId(), commonLanguageProperties.getResourceId()+commonLanguageProperties.getNotNull());
        Assert.hasText(executionTaskModel.getType(), commonLanguageProperties.getResourceType()+commonLanguageProperties.getNotNull());
        if (!isNotVersioned(executionTaskModel.getType())) {
            Assert.notNull(executionTaskModel.getVersion(), commonLanguageProperties.getResourceVersion()+commonLanguageProperties.getNotNull());
        }
        Assert.notNull(executionTaskModel.getAuditFlag(), "AuditFlag"+commonLanguageProperties.getNotNull());
        Assert.hasText(executionTaskModel.getAuditDesc(), "AuditDesc"+commonLanguageProperties);
    }

    /**
     * 校验资源ID-类型
     *
     * @param id
     * @param type
     */
    public static void validIdAndType(Integer id, String type) {
        Assert.notNull(id, commonLanguageProperties.getResourceId()+commonLanguageProperties.getNotNull());
        Assert.hasText(type, commonLanguageProperties.getResourceType()+commonLanguageProperties.getNotNull());
    }

    /**
     * 没有版本的资源
     *
     * @param type
     * @return
     */
    private static boolean isNotVersioned(String type) {
        return ResourceTypeEnum.LAYOUT.getValue().equals(type)
                || ResourceTypeEnum.CORNER_ICON.getValue().equals(type)
                || ResourceTypeEnum.POSTER.getValue().equals(type);
    }

    @Autowired
    public void setEnterKeyLetterArt(CommonLanguageProperties commonLanguageProperties) {
        AuditTaskValidator.commonLanguageProperties = commonLanguageProperties;
    }
}
